package sdec.DisplayPhotoServlet;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DisplayPhotoServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        try {
	    	Class.forName("com.mysql.jdbc.Driver");
	    	Connection con = DriverManager.getConnection("jdbc:mysql://localhost/directorioprofesional", "root", "");
	        PreparedStatement ps = con.prepareStatement("select imagen from users where username = ?");
            String username = "";
			if(request.getParameter("username")!=null){
				username = request.getParameter("username");
			}
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
	            Blob  image = rs.getBlob("imagen");
	            response.setContentType("image/png");
	            response.setContentLength( (int) image.length());
	            byte[] imgData = image.getBytes(1,(int)image.length());
	            OutputStream os = response.getOutputStream();
	            os.write(imgData);
	            os.flush();
	            os.close();
            }
        }
        catch(Exception e) {
        	e.printStackTrace();
        }
    } 
}